package nl.hu.verbrandendephoenix.controller;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.server.UserMethods;
import nl.hu.verbrandendephoenix.shared.FieldVerifier;

public class BuyCredits {

	private nl.hu.verbrandendephoenix.view.BuyCredits buyCredits;
	final FieldVerifier fv = FieldVerifier.get();
	final NotificationManager nM = NotificationManager.get();
	public int aantCreds;

	public BuyCredits(){
		
	}

	public Panel getPanel(){
		buyCredits = new nl.hu.verbrandendephoenix.view.BuyCredits();
		Panel p = buyCredits.ConstructPanel();
		addHandlers();
		return p;

	}
	
	public void addHandlers(){
		
		Button btnSubmit1 = buyCredits.getbtnSubmit1();
		btnSubmit1.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e){
				int keuze = buyCredits.lbxCreds.getSelectedIndex();
				if(keuze == 0){
					nM.createNotification("U moet een hoeveelheid credits selecteren!");
				}if(keuze == 1){
					aantCreds = 1;				
				}if(keuze == 2){
					aantCreds = 10;
				}if(keuze == 3){
					aantCreds = 25;
				}if(keuze == 4){
					aantCreds = 50;
				}if(keuze == 5){
					aantCreds = 100;
				}if(keuze == 6){
					aantCreds = 250;
				}if(keuze == 7){
					aantCreds = 500;
				}if(keuze == 8){
					aantCreds = 1000;
				}
				
				buyCredits.lblCreds.setVisible(false);
				buyCredits.lbxCreds.setVisible(false);
				buyCredits.lbxCreds.setEnabled(false);
				buyCredits.btnSubmit1.setVisible(false);
				buyCredits.btnSubmit1.setEnabled(false);
				buyCredits.btnSubmit2.setVisible(false);
				buyCredits.btnSubmit2.setEnabled(false);
				buyCredits.btnCancel.setVisible(false);
				buyCredits.lblCreds2.setVisible(false);
				buyCredits.txtCreds.setVisible(false);
				buyCredits.txtCreds.setEnabled(false);				
				buyCredits.btnCancel.setEnabled(false);
				
				buyCredits.lblConfirm.setText("Weet u zeker dat u " + aantCreds + " wilt kopen?");
				buyCredits.lblConfirm.setVisible(true);
				buyCredits.grdCredForm.setWidget(0, 0, buyCredits.lblConfirm);
				buyCredits.grdCredForm.setWidget(1, 1, buyCredits.btnSubmit3);
				buyCredits.grdCredForm.setWidget(1, 2, buyCredits.btnCancel2);
	
				
			}
		});
		
		Button btnSubmit2 = buyCredits.getbtnSubmit2();
		btnSubmit2.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(buyCredits.txtCreds.getText() == ""){ 
				nM.createNotification("U moet wel een aantal credits invullen!");
			
				}else{
					aantCreds = Integer.parseInt(buyCredits.txtCreds.getText());
					
					
					buyCredits.lblCreds.setVisible(false);
					buyCredits.lbxCreds.setVisible(false);
					buyCredits.lbxCreds.setEnabled(false);
					buyCredits.btnSubmit1.setVisible(false);
					buyCredits.btnSubmit1.setEnabled(false);
					buyCredits.btnSubmit2.setVisible(false);
					buyCredits.btnSubmit2.setEnabled(false);
					buyCredits.btnCancel.setVisible(false);
					buyCredits.lblCreds2.setVisible(false);
					buyCredits.txtCreds.setVisible(false);
					buyCredits.txtCreds.setEnabled(false);				
					buyCredits.btnCancel.setEnabled(false);
					
					buyCredits.lblConfirm.setText("Weet u zeker dat u " + aantCreds + " credits wilt kopen?");
					buyCredits.lblConfirm.setVisible(true);
					buyCredits.grdCredForm.setWidget(0, 0, buyCredits.lblConfirm);
					buyCredits.grdCredForm.setWidget(1, 1, buyCredits.btnSubmit3);
					buyCredits.grdCredForm.setWidget(1, 2, buyCredits.btnCancel2);
				}
			}
			
		});
		
		Button btnSubmit3 = buyCredits.getbtnSubmit3();
		btnSubmit3.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				processSubmit(aantCreds);
				nM.createNotification(aantCreds + " is aan Uw account toegevoegd");
				History.newItem("searchauction");
				
			}
			
		});
		
		Button btnCancel = buyCredits.getbtnCancel();
		btnCancel.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				History.newItem("searchauction");
				
			}
			
		});
		
		Button btnCancel2 = buyCredits.getbtnCancel2();
		btnCancel2.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				History.newItem("searchauction");
				
			}
			
		});
			
		
	}
	
	public void processSubmit(final int aantCreds){
		
		AppManager.get().getRpcService().addCredits(aantCreds,(Cookies.getCookie("loginString")), new AsyncCallback<Boolean>(){

			@Override
			public void onFailure(Throwable caught) {
				nM.commError();
				
			}

			@Override
			public void onSuccess(Boolean result) {
				nM.createNotification(aantCreds + " zijn aan uw account toegevoegd!");
				
			}
			
		});
	}
}
